﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserInterface.UserControls
{
    using Presenters;

    /// <summary>
    /// Interaction logic for MapElement.xaml
    /// </summary>
    public partial class MapElement : UserControl
    {
        #region variables

        private ObjectOnMap Settlement = null;
        private ObjectOnMap Unit = null;

        internal bool HaveSettlement
        {
            get
            {
                if (this.Settlement == null)
                    return false;
                else
                    return true;
            }
        }
        internal bool IsSettlementHidden
        {
            get
            {
                if (this.Settlement.Visibility == System.Windows.Visibility.Hidden)
                    return true;
                else
                    return false;
            }
        }
        internal bool HaveUnit
        {
            get
            {
                if (this.Unit == null)
                    return false;
                else
                    return true;
            }
        }
        internal bool IsUnitHidden
        {
            get
            {
                if (this.Unit.Visibility == System.Windows.Visibility.Hidden)
                    return true;
                else
                    return false;
            }
        }

        #endregion

        #region constuctor

        public MapElement(
            ImageBrush tileImageBrush,
            ImageBrush settlementImageBrush = null,
            ImageBrush unitImageBrush = null
            )
        {
            InitializeComponent();
            grid.Background = tileImageBrush;
            if (settlementImageBrush != null)
                this.CreateSettlement(settlementImageBrush);
            if (unitImageBrush != null)
                this.CreateUnit(unitImageBrush);
        }

        #endregion

        #region set settlement and unit into grid

        private void SetSettlementIntoGrid()
        {
            Grid.SetColumn(this.Settlement, 0);
            Grid.SetRow(this.Settlement, 0);
            this.ContentGrid.Children.Add(this.Settlement);
        }

        private void SetUnitIntoGrid()
        {
            Grid.SetColumn(this.Unit, 1);
            Grid.SetRow(this.Unit, 1);
            this.ContentGrid.Children.Add(this.Unit);
        }

        #endregion

        #region tile
        internal void SetTileContextMenu(ContextMenu menu)
        {
            this.grid.ContextMenu = menu;
        }
        #endregion

        #region settlement

        internal void CreateSettlement(ImageBrush imageBrush)
        {
            this.Settlement = new ObjectOnMap(imageBrush);
            this.SetSettlementIntoGrid();
        }

        internal void SetSettlementContextMenu(ContextMenu contextMenu)
        {
            this.Settlement.ContextMenu = contextMenu;
        }

        internal void SetSettlementContent(ImageBrush imageBrush)
        {
            this.Settlement.Content = imageBrush;
        }

        internal void SetSettlementForground(ImageBrush imageBrush)
        {
            this.Settlement.Foreground = imageBrush;
        }

        internal void SetSettlementHidden()
        {
            this.Settlement.Visibility = System.Windows.Visibility.Hidden;
        }

        internal void SetSettlementVisible()
        {
            this.Settlement.Visibility = System.Windows.Visibility.Visible;
        }

        #endregion

        #region unit

        internal void CreateUnit(ImageBrush imageBrush)
        {
            this.Unit = new ObjectOnMap(imageBrush);
            this.SetUnitIntoGrid();
        }

        internal void SetUnitContextMenu(ContextMenu contextMenu)
        {
            this.Unit.ContextMenu = contextMenu;
        }

        internal void SetUnitContent(ImageBrush imageBrush)
        {
            this.Unit.Content = imageBrush;
        }

        internal void SetUnitForegroud(ImageBrush imageBrush)
        {
            this.Unit.Foreground = imageBrush;
        }

        internal void SetUnitHidden()
        {
            this.Unit.Visibility = System.Windows.Visibility.Hidden;
        }

        internal void SetUnitVisible() 
        {
            this.Unit.Visibility = System.Windows.Visibility.Visible;
        }

        #endregion

        #region Get Button Position

        internal void GetButtonPosition(Button button, out int x, out int y)
        {
            if (button == this.Settlement.MapObject)
            {
                x = 0;
                y = 0;
            }

            if (button == this.Unit.MapObject)
            {
                x = 1; y = 1;
            }

            throw new ArgumentException("button not exist in this map element.");
        }


        #endregion

    }
}
